import Cookies from 'js-cookie'
import {Modal} from "antd";

export function trim(str: string) {
  return str.trim();
}

export function getUserInfo() {
  try {
    return JSON.parse(Cookies.get('userInfo'))
  } catch {
    return {}
  }
}

export function isLogin() {
  const userInfo = getUserInfo()
  return !!userInfo.token
}

//静态菜单数据添加填报和周期flag
export function MergeMenu(data, response) {
  for (let i = 0; i < data.length; i++) {
    for (let j = 0; j < response.menus.length; j++) {
      if (data[i].key === response.menus[j].id) {
        data[i].is_period = response.menus[j].is_period
        data[i].is_filled = response.menus[j].is_filled
        data[i].circle = response.menus[j].circle ? response.menus[j].circle : data[i].circle
        data[i].name = response.menus[j].name ? response.menus[j].name : data[i].name
        break
      }
    }
  }
  return data
}

// xxxx-xx-xx 获取 年
export function getYear(yr) {
  if (!yr) return yr
  if (yr._isAMomentObject) {
    yr = String(yr._i)
  }
  if (yr.split('-').length === 1)
    return yr
  return yr.split('-')[0]
}

// xxxx-xx-xx 获取 年-月
export function getYearMonth(yr) {
  if (!yr) return yr
  if (yr._isAMomentObject) {
    yr = String(yr._i)
  }
  console.log("@yr after", yr)
  if (yr.split('-').length === 2) {
    console.log("@yr after", yr)
    return yr
  }
  let t = yr.split('-')
  t.pop()
  return t.join('-')
}

// [xxxx-xx-xx, xxxx-xx-xx] ==> xxxx-xxxx
export function getYearFromRange(yr) {
  if (Array.isArray(yr))
    return getYear(yr[0]) + "-" + getYear(yr[1])
  return yr
}

// xxxx-xx至xxxx-xx
export function getYearFromRangeShit(yr) {
  if (Array.isArray(yr))
    return getYearMonth(yr[0]) + "至" + getYearMonth(yr[1])
  return yr
}

export function ShowError(msg) {
  Modal.error({
    title: msg,
  });
}

// 省厅 - 查看所有table的详情 - 生成 url
export const ProvinceAllUrl = (school, subject, point, point_name) => {
  return '/province/table/detail?school='+school+'&subject='+subject+'&point='+point+'&pointname='+point_name
}
